package com.zj.healthmanage.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zj.healthmanage.entity.PageResult;
import com.zj.healthmanage.entity.QueryPageBean;
import com.zj.healthmanage.mapper.CheckgroupCheckitemMapper;
import com.zj.healthmanage.pojo.CheckgroupCheckitem;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.zj.healthmanage.mapper.CheckgroupMapper;
import com.zj.healthmanage.pojo.Checkgroup;
import com.zj.healthmanage.service.CheckgroupService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CheckgroupServiceImpl implements CheckgroupService{

    @Resource
    private CheckgroupCheckitemMapper checkgroupCheckitemMapper;

    @Resource
    private CheckgroupMapper checkgroupMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return checkgroupMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Checkgroup record) {
        return checkgroupMapper.insert(record);
    }

    @Override
    public int insertSelective(Checkgroup record,Integer[] checkitemIdList) {
        Integer checkgroupId = record.getId();
        setCheckGroupAndCheckItem(checkgroupId,checkitemIdList);
        return checkgroupMapper.insertSelective(record);
    }

    @Override
    public Checkgroup selectByPrimaryKey(Integer id) {
        return checkgroupMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Checkgroup record,Integer[] checkitemIdList) {
        Integer checkgroupId = record.getId();
        checkgroupCheckitemMapper.deleteCheckGroupAndCheckItem(checkgroupId);
        setCheckGroupAndCheckItem(checkgroupId,checkitemIdList);
        return checkgroupMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Checkgroup record) {
        return checkgroupMapper.updateByPrimaryKey(record);
    }


    private void setCheckGroupAndCheckItem(Integer checkGroupId,Integer[] checkitemIdList){
        if (checkGroupId != null && checkitemIdList.length > 0){
            for (Integer checkitemId : checkitemIdList) {
                CheckgroupCheckitem checkgroupCheckitem = new CheckgroupCheckitem();
                checkgroupCheckitem.setCheckgroupId(checkGroupId);
                checkgroupCheckitem.setCheckitemId(checkitemId);
                checkgroupCheckitemMapper.insert(checkgroupCheckitem);
            }
        }
    }

    @Override
    public PageResult queryCheckGroupByPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Checkgroup> page = checkgroupMapper.queryCheckGroupByPage(queryString);
        long total = page.getTotal();
        List<Checkgroup> rows = page.getResult();
        return new PageResult(total,rows);
    }


    @Override
    public List<Checkgroup> queryAll() {
        return checkgroupMapper.queryAll();
    }

    @Override
    public List<Integer> queryCheckItemIdsById(Integer id) {
        return checkgroupCheckitemMapper.queryCheckItemIdsById(id);
    }

}
